package com.atguigu.flink.chapter07.state;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.FunctionInitializationContext;
import org.apache.flink.runtime.state.FunctionSnapshotContext;
import org.apache.flink.streaming.api.checkpoint.CheckpointedFunction;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

import java.util.ArrayList;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/8/16 15:03
 */
public class Flink01_Operator_List {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);
        env.enableCheckpointing(3000);
        
        env
            .socketTextStream("hadoop162", 9999)
            .flatMap(new MyFlatMap())
            .print();
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static class MyFlatMap implements FlatMapFunction<String, String>, CheckpointedFunction {
        
        ArrayList<String> words = new ArrayList<>();
        private ListState<String> wordsListState;
    
        @Override
        public void flatMap(String value, Collector<String> out) throws Exception {
            if (value.contains("a")) {
                throw new RuntimeException("手动抛出一个异常");
            }
    
            // 把每个并行度收到的单词存入到一个集合中
            for (String word : value.split(" ")) {
                words.add(word);
                out.collect(words.toString());
            }
    
            
        }
        
        // 保存状态. 把数据保存状态中, flink负责把这些状态给具体的存储
        @Override
        public void snapshotState(FunctionSnapshotContext ctx) throws Exception {
//            System.out.println("MyFlatMap.snapshotState");
            // 把ArrayList中的数据,存入到算子状态, 这样flink会自动给保存, 当异常重启的时候, 可以恢复状态
            
            /*wordsListState.clear();
            wordsListState.addAll(words);*/
    
            wordsListState.update(words);
            
        }
        
        // 初始化状态:  从状态中的恢复数据
        @Override
        public void initializeState(FunctionInitializationContext ctx) throws Exception {
            System.out.println("MyFlatMap.initializeState");
            wordsListState = ctx.getOperatorStateStore().getListState(new ListStateDescriptor<String>("words", String.class));
            
            // 把状态中的数据取出, 存储到ArrayList中
            Iterable<String> it = wordsListState.get();
            for (String word : it) {
                System.out.println("恢复: " + word);
                words.add(word);
            }
        }
        
    }
}
